<?php
/**
 * Created by PhpStorm.
 * User: 86188
 * Date: 2021/3/6
 * Time: 9:37
 */

namespace app\admin\controller;


use app\service\roominfoService;
use app\service\wxtplService;
use think\facade\Db;
use think\facade\View;

class Roomcheck extends Common
{
    public function index(){
        //查询小区信息
        $com = $this->pidTokenId();

        //$whe['community_id'] = array('in',$com['community_id']);
        $whe[] = ['community_id','in',$com['community_id']];

        $communitylist = (new roominfoService())->communityList($whe);
        View::assign('communitylist',$communitylist);

        $where=array();
        $page = request()->has('page') ? request()->get('page') : 1;

        $roomlist =Db::name('roomcheck')
            ->where('community_id', 'in', $com['community_id'])
            ->order('id desc')->paginate(200, false, [
                'page'  =>  $page,
                'query' =>  $where
            ]);

        $render=$roomlist->render();
        foreach ($roomlist as $roominfo){
            $roominfo['community_name'] = (new roominfoService())->communityName($roominfo['community_id']);
            $roominfo['address'] = Db::name('roominfo')->where('id',$roominfo['roomid'])->value('address');
            $list[] = $roominfo;
        }
        if(isset($list)){
            View::assign('list',$list);
        }else{
            View::assign('list',$roomlist);
        }

        View::assign("render",$render);
        return View::fetch();
    }

    public function create(){
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();

            //Log::write($data,"roominfo");

            $roominfo=Db::table("roominfo")
                        ->where("community_id",$data["community_id"])
                        ->where("group",$data["group"])
                        ->where("building",$data["building"])
                        ->where("unit",$data["unit"])
                        ->where("room",$data["room"])
                        ->find();
            if(empty($roominfo)){
                return "房屋信息不存在";
            }
            if(($roominfo["owner_name"]==$data["owner_name"])&&($roominfo["owner_phone"]==$data["owner_phone"])){
                return "房屋信息未修改";
            }
            $roomcheck["community_id"]=$roominfo["community_id"];
            $roomcheck["roomid"]=$roominfo["id"];
            $roomcheck["owner_name_before"]=$roominfo["owner_name"];
            $roomcheck["owner_phone_before"]=$roominfo["owner_phone"];

            $roomcheck["owner_name"]=$data["owner_name"];
            $roomcheck["owner_phone"]=$data["owner_phone"];


            $roomcheck["create_time"]=time();
            $roomcheck["username"]=session("name");

            $resid=Db::table("roomcheck")->insertGetId($roomcheck);
            if($resid){
                $wxuserlist=Db::table("community_wxuser")->where("community_id",$data["community_id"])->select()->toArray();

                foreach ($wxuserlist as $userinfo){
                    $tenpalate_id="OazWbiMUE_-NM70xR6DvVDpMNQlHpZsbEVy_AKmzZbo";
                    $url="https://www.wuye.zfbzhsq.com/index/roomcheck/index?id=".$resid;
                    $communityinfo=Db::table("community")->where("community_id",$roominfo["community_id"])->find();
                    $first=$communityinfo["community_name"]."-".$roominfo["address"];
                    $keyword1="小区有房屋信息需要修改";
                    $keyword3=session("name");
                    $remark="请尽快帮我审核";
                    $ctime=date("Y-m-d H:i:s");

                    $data=(new wxtplService())->json_tempalte_review($userinfo["openid"],$tenpalate_id,$url,$first,$keyword1,$ctime,$keyword3,$ctime,$remark);
                    $access_token=(new Basewxget())->gettoken();
                    $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
                    $res=(new wxtplService())->curl_post($url,$data);
                    //Log::write($res,"wxusermsgt");
                }
                return "ok";
            }else{
                return "房屋修改信息提交失败";
            }
        }else{
            //查询小区信息
            $com = $this->pidTokenId();
            $whe['community_id'] = array('in',$com['community_id']);
            $communitylist = (new roominfoService())->communityList($whe);
            $community_id = $com['community_id'];
            View::assign('communitylist',$communitylist);

            return View::fetch();
        }

    }
}